package org.yanghaokun.pet.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yanghaokun.basic.service.impl.BaseServiceImpl;
import org.yanghaokun.basic.util.CodeGenerateUtils;
import org.yanghaokun.basic.util.DistanceUtil;
import org.yanghaokun.basic.util.PageList;
import org.yanghaokun.basic.util.Point;
import org.yanghaokun.order.domain.PetAcquisitionOrder;
import org.yanghaokun.order.mapper.PetAcquisitionOrderMapper;
import org.yanghaokun.org.domain.Employee;
import org.yanghaokun.org.domain.Shop;
import org.yanghaokun.org.mapper.EmployeeMapper;
import org.yanghaokun.org.mapper.ShopMapper;
import org.yanghaokun.pet.domain.Pet;
import org.yanghaokun.pet.domain.SearchMasterMsg;
import org.yanghaokun.pet.mapper.SearchMasterMsgMapper;
import org.yanghaokun.pet.query.SearchMasterMsgQuery;
import org.yanghaokun.pet.service.IPetService;
import org.yanghaokun.pet.service.ISearchMasterMsgService;
import org.yanghaokun.user.domain.User;
import org.yanghaokun.user.mapper.UserMapper;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg msg, Long id) {
        //用户发布了消息需要找到最近的店铺,从所有的店铺中查找
        Point point = DistanceUtil.getPoint(msg.getAddress());
        Shop shop = DistanceUtil.getNearestShop(point, shopMapper.findAll());
        //绑定店铺
        msg.setShop_id(shop.getId());
        //根据登陆信息找到用户
        //绑定用户信息
        User user = userMapper.findByloginId(id);
        msg.setUser_id(user.getId());
        //将消息持久化处理
        searchMasterMsgMapper.save(msg);
        //发送短信息给店铺管理员
        Long bossId = shop.getAdmin_id();
        Employee boss = employeeMapper.findOne(bossId);
        System.out.println(boss.getPhone()+"老板，来["+msg.getTitle()+"]生意了！赶快去接客！");
    }

    /**
     * //待处理消息-平台管理员或店铺管理员（员工）
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        //因为是未处理的信息，所以这里的状态设置为0
        query.setState(0);
        //我们在这里判断登陆的是店铺员工或店铺管理员还是平台管理员
        //那么就看logingInfo里面的userId来查询员工信息，看看是否有shopId
        Employee employee = employeeMapper.findByloginInfoId(id);
        if (employee.getShop_id() != null) {
            //是员工或则是店铺管理员只给他们看它们店铺接收到的信息
            //在查询条件这种设置当前店铺的店铺id
            query.setShopId(employee.getShop_id());
        }
        return super.getList(query);
    }

    /**
     *已处理消息-平台管理员或店铺管理员（员工）
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        //因为是已处理的信息，所以这里的状态设置为1
        query.setState(1);
        //我们在这里判断登陆的是店铺员工或丹普管理员还是频台管理员
        //那么就看logingInfo里面的userId来查询员工信息，看看是否有shopId
        Employee employee = employeeMapper.findByloginInfoId(id);
        if (employee.getShop_id() != null) {
            //是员工或则是店铺管理员只给他们看它们店铺接收到的信息
            //在查询条件这种设置当前店铺的店铺id
            query.setShopId(employee.getShop_id());
        }
        return super.getList(query);
    }

    /**
     * 查看用户发布消息
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> query(SearchMasterMsgQuery query, Long id) {
        //用户的只能看见胡自己发布的信息
        //我们直接通过登录信息查找用户
        User user = userMapper.findByloginId(id);
        query.setUserId(user.getId());
        return super.getList(query);
    }

    @Override
    public void handle(Pet pet,Long loginInfoId) {
        //处理了这次消息之后我们需要将其转换状态为已处理，其次我们还需要添加一些其他的额外信息
        Long searchMasterMsgId = pet.getSearchMasterMsgId();
        //通过id修改状态为已处理
        searchMasterMsgMapper.updateStateById(searchMasterMsgId);
        //添加到已处理，因为这次我们处理需要多添加宠物和宠物详情的信息所以我们用service来处理
        petService.add(pet);
        //3创建订单
        Employee employee = employeeMapper.findByloginInfoId(loginInfoId);
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(searchMasterMsgId);
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, msg, employee.getId());
        petAcquisitionOrderMapper.save(petAcquisitionOrder);

        //4 @TODO 如果要收钱，创建支付单并付完款
        if (petAcquisitionOrder.getPrice().longValue()>0){
            System.out.println("你已经付款了。。。。。");
        }
    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg msg,Long employeeId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(msg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(msg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(msg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(employeeId);
        return order;
    }

}
